package com.huawei.hwid.core.d;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.text.TextUtils;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.hwid.core.d.b.e;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

public class a {
    private static Map<String, com.huawei.hwid.core.datatype.a> a = new HashMap();
    private static String b = "";

    private static synchronized void a(Context context) {
        synchronized (a.class) {
            try {
                a = b(context);
            } catch (Exception e) {
                e.d("AppInfoUtil", "initAppInfos error", true);
            }
        }
    }

    public static String a(Context context, String str) {
        String a;
        String c = c(context, str);
        String str2 = "";
        if (((com.huawei.hwid.core.datatype.a) a.get(c)) != null) {
            a = ((com.huawei.hwid.core.datatype.a) a.get(c)).a();
        } else {
            a = str2;
        }
        if (TextUtils.isEmpty(a)) {
            a = "7000000";
        }
        try {
            Integer.parseInt(a);
        } catch (NumberFormatException e) {
            e.d("AppInfoUtil", "NumberFormatException", true);
            a = "7000000";
        }
        e.b("AppInfoUtil", "getAppChannel", true);
        return a;
    }

    public static String a() {
        return c() + "AB09070647056445";
    }

    public static String b(Context context, String str) {
        b = str;
        return c() + b;
    }

    private static String c() {
        return "99E790F6FBA9FDA8";
    }

    public static String b() {
        if (TextUtils.isEmpty(b)) {
            byte[] bArr = new byte[8];
            new SecureRandom().nextBytes(bArr);
            b = b.a(bArr);
        }
        return b;
    }

    private static String c(Context context, String str) {
        if (TextUtils.isEmpty(str) || "com.huawei.hwid".equalsIgnoreCase(str)) {
            str = "com.huawei.hwid";
        }
        if (a == null || a.isEmpty()) {
            a(context);
        }
        return str;
    }

    private static Map<String, com.huawei.hwid.core.datatype.a> b(Context context) {
        e.b("AppInfoUtil", "initAppInfos", true);
        XmlResourceParser xml = context.getResources().getXml(l.b(context, "appinfo"));
        Map<String, com.huawei.hwid.core.datatype.a> hashMap = new HashMap();
        if (xml != null) {
            try {
                com.huawei.hwid.core.datatype.a aVar = new com.huawei.hwid.core.datatype.a();
                for (int eventType = xml.getEventType(); 1 != eventType; eventType = xml.next()) {
                    String name = xml.getName();
                    switch (eventType) {
                        case 2:
                            if (!"appID".equals(name)) {
                                if (!CloudAccountManager.KEY_REQCLIENTTYPE.equals(name)) {
                                    if (!"defaultChannel".equals(name)) {
                                        break;
                                    }
                                    aVar.b(xml.nextText());
                                    break;
                                }
                                aVar.a(xml.nextText());
                                break;
                            }
                            aVar.c(xml.nextText());
                            break;
                        case 3:
                            if (!"appInfo".equals(name)) {
                                break;
                            }
                            hashMap.put(aVar.b(), aVar);
                            aVar = new com.huawei.hwid.core.datatype.a();
                            break;
                        default:
                            break;
                    }
                }
            } catch (RuntimeException e) {
                e.d("AppInfoUtil", "initAppInfos error RuntimeException", true);
            } catch (Exception e2) {
                e.d("AppInfoUtil", "initAppInfos error", true);
            } finally {
                xml.close();
            }
        }
        return hashMap;
    }
}
